package com.bh.ly.oa.activity;

import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.bh.ly.oa.BaseActivity;
import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.MenuAdapter;

public class IndexActivity extends BaseActivity {

	private long lastKeyBack = 0;
	private TextView txtMsg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initViews();
		initDatas();
		setListener();
	}
	
	public void initDatas() {	
		txtMsg.setText("你好，欢迎进入供应链系统,今天是"+DateFormat.format("yyyy-MM-dd EEEE", new Date()));
	}

	public void initViews() {
		setContentView(R.layout.index);
		((TextView) findViewById(R.id.title_textview)).setText("供应链系统");
		txtMsg = (TextView) findViewById(R.id.index_msg);
		
		GridView gridView = (GridView) findViewById(R.id.lineary_yinzhang);
		
		gridView.setAdapter(new MenuAdapter(this));
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(IndexActivity.this, ItemActivity.class);
				intent.putExtra("index", arg2);
				startActivity(intent);
			}
			
		});
		
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public void setListener() {
		// TODO Auto-generated method stub
	}
	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
		{
			long systemTime = System.currentTimeMillis();

			if (systemTime - lastKeyBack < 3000)
			{
				finish();
				System.exit(0);
			} else
			{
				lastKeyBack = systemTime;
				Toast.makeText(this, "再按一下返回键退出程序！", Toast.LENGTH_SHORT).show();
			}
			return true;
		}

		return super.dispatchKeyEvent(event);
	}	
}
